global root_dir = "`1'"

include "$root_dir/code/config/config.do"


cap noi log using ${log_dir}/cleaning_and_adjustments_wages.log, replace name(dat)

*Handle empty arguments
global arg1 = cond("`2'" == "___EMPTY___", "", "`2'")
global arg2 = cond("`3'" == "___EMPTY___", "", "`3'")
global arg3 = cond("`4'" == "___EMPTY___", "", "`4'")
global arg4 = cond("`5'" == "___EMPTY___", "", "`5'")

if "$arg1" != "" {
    global weight_category "$arg1"
    di "Weight category: ${weight_category}"
}

if "$arg2" != "" {
    global weight_versions "$arg2"
    di "Weight versions: ${weight_versions}"
}

if "$arg3" != "" {
    global weight_window "$arg3"
    di "Weight window: ${weight_window}"
}

if "$arg4" != "" {
	global wtype "$arg4"
}
di "${wtype}"
capture noi {

***********************************************
**************	WAGE MEASURES *****************
***********************************************

*This code combines the seperate parts, makes some adjustments for exchange rates and cleanes some more missing observations.
*Data used is 

* (i) World Input Output Tables: 
* average low-skill, high-skill wages from 1995 to 2009-2011 (depending on 
* country) Note: this is the 2013 version. There is a 2016 version, but that 
* does not have wages by skill-content. Hourly

* (ii) EUKLEMS: 
* average low-skill wages, high-skill wages: until 2005 not back until 1970 
* for all average wages.  Wages across skill-category and industry between 1970 and 1995 (depending on country) to around 2005. Hourly.

* (iii) Minimum wages: added after everything else is run
* Mainly from OECD. Germany, UK, parts of IE and US added separately. For all countries except

* (iv) Exchange rates:
* from EUSTATS

* (v) GDP measurements: 
* from UNSTATS

* (vi) MGO: March 2019: We have added Switzerland separately. 
* See details in "import_swiss_data.do"

* The WIOD will be our baseline. The majority of the work is done in terms of getting EUklems to speak to WIOD. This includes
* - Exchange rate conversion
* - Various "fixes" when certain economies look weird (e.g. Italy).


***********************************************
**************	1. CLEANING OF DATA ***********
***********************************************

use ${mow_data_proc}/wage_combined.dta, clear

***********************************************************************
*1.a Impute variables for 2003 in the WIOD data which has an odd spike*
***********************************************************************

sort country code year
foreach var in ls ms hs lms {
    replace `var'_wage_WIOD=`var'_wage_WIOD[_n-1]/2+`var'_wage_WIOD[_n+1]/2 if year==2003
}
	
*******************************
*1.b Combine WIOD and EUklems**
*******************************
	
sort country code year

* Generate the combination of WIOD and EUklems as "_new" variables. 
	
* Make sure that the level is the same in 1995 regardless of whether data comes from WIOD or EUklems. 
foreach wage_level in ls_wage ms_wage hs_wage av_wage lms_wage {
    by country code:gen scale_factor_2=`wage_level'/`wage_level'_WIOD if year==1995
    by country code:egen scale_factor=total(scale_factor_2)
    gen `wage_level'_new=`wage_level'_WIOD
    replace `wage_level'_new=`wage_level'/scale_factor if `wage_level'_new==.
    drop scale_factor*	
}

* Same quality variables for low-skill and high-skill wages. Generate dummies for each quality. 
gen quality=1
replace quality=2 if ls_wage_WIOD==.
replace quality=3 if ls_wage_WIOD==. & ls_wage==.
label variable quality "Source of low-skill data (1: From WIOD, 2: Rescaled from EUklems, 3: No data)"


* Price deflator. Make sure that same level in 1995 regardless of origin of data. 
sort country code year
by country code:gen scale_factor_2=VA_P/VA_P_WIOD if year==1995
by country code:egen scale_factor=total(scale_factor_2)
gen VA_P_new=VA_P_WIOD
replace VA_P_new=VA_P/scale_factor if VA_P_new==.

drop scale_factor*

* Value added per employee. Make sure that same level in 1995 regardless of origin of data.
sort country code year
by country code:gen scale_factor_2=va_emp/va_emp_WIOD if year==1995
by country code:egen scale_factor=total(scale_factor_2)
gen va_emp_new=va_emp_WIOD
replace va_emp_new=va_emp/scale_factor if va_emp_new==.
drop scale_factor*

* Rename and label variables. Introduce now _EUklems for data coming from _EUklems. 
* Combination of EUklems and WIOD  is now without subscript. 
rename ls_wage ls_wage_EUklems
rename lms_wage lms_wage_EUklems
rename av_wage av_wage_EUklems
rename VA_P VA_P_EUklems
rename va_emp va_emp_EUklems
rename ms_wage ms_wage_EUklems
rename hs_wage hs_wage_EUklems
rename ls_wage_new ls_wage
rename lms_wage_new lms_wage
rename av_wage_new av_wage
rename VA_P_new VA_P
rename va_emp_new va_emp
rename hs_wage_new hs_wage
rename ms_wage_new ms_wage
label variable ls_wage "low-skill wages own current currency"
label variable lms_wage "low/middle-skill wages"
label variable hs_wage "high-skill wages own current currency"
label variable VA_P "Producer Price index"
label variable va_emp "Value added per employee"
label variable code_share "Share of manufacturing in total value added"
label variable ms_wage "middle-skill wages own current currency"
label variable av_wage "average wages own current currency"
label variable code_VA "Value added in sector"

save ${mow_data_proc}/wage_combined_cleaned.dta, replace 

******************
*1.c Downsize data
******************




    use ${mow_data_proc}/wage_combined_cleaned.dta, clear 
    keep if year >= 1975    

    ***********************************************
    *******	2. CREATE VARIABLES OF INTEREST *******
    ***********************************************

    *we later drop observation if observations in a specific deflator are missing, so this might cause problems

    *2.a. 	Variables with 2005 fixed exchange rates and deflated by PPI or the 
    *		GDP deflator
    *		***_pi_USD_05 and ***_defGDP_USD_05 -> LP and  LG

    *2.b.	Variables with 1995 fixed exchange rates and deflated by PPI or the 
    *		GDP deflator
    *		***_pi_USD_95 and ***_defGDP_USD_95 -> MP and MG


    *2.c.	Variables every year exchange rate & deflated by PPI US or GDP deflator US 
    *		***_USD_pi and ***_USD_defGDP

    *2.d	Real GDP (total & capita) converted into USD (1995 and 2005) (note: other GDP variables also in 2.a. - 2.c.) 
    *		***_05_USD_05 and ***_95_USD_95

    *2.c	Robustness variables
    *		ls_wage_USD_05
    *		tot_pay_ls_USD_95 



    ************************************************************************
    **2.a. Variables with 2005 fixed exchange rates (PPI & GDP deflator)****
    ************************************************************************
    *Deflated by PPI and local GDP deflator. 


    *2.a.i. Deflate by PPI 
    foreach var in ls_wage lms_wage ms_wage hs_wage av_wage va_emp GDP_nom_own cap_GDP_nom_own {
        gen `var'_pi_05=`var'/VA_P*100
    }

    label variable ls_wage_pi_05 "low-skill wages  -  deflated by pi.05 "
    label variable hs_wage_pi_05 "high-skill wages  -  deflated by pi.05 "
    label variable av_wage_pi_05 "average wages  -  deflated by pi.05 "
    label variable lms_wage_pi_05 "low-skill/middle-skill wages  - . deflated by pi.05 "
    label variable ms_wage_pi_05 "middle-skill wages  - . deflated by pi.05 "
    label variable va_emp_pi_05 "labor productivity (VA/H_EMP) -  deflated by pi.05 "
    label variable GDP_nom_own_pi_05 "Nominal GDP local currency-  deflated by pi.05 "
    label variable cap_GDP_nom_own_pi_05 "Nominal GDP per capita local currency -  deflated by pi.05 "



    *2.a.ii. Deflate by local GDP deflator. 
    foreach var in ls_wage lms_wage ms_wage hs_wage av_wage va_emp {
        gen `var'_defGDP_05=`var'/def_GDP_own_05*100
    }


    label variable ls_wage_defGDP_05 "low-skill wages  -  deflated by local GDP deflator 05   "
    label variable hs_wage_defGDP_05 "high-skill wages  -  deflated by local GDP deflator 05  "
    label variable av_wage_defGDP_05 "average wages  -  deflated by local GDP deflator 05  "
    label variable lms_wage_defGDP_05 "low-skill/middle-skill wages  - . deflated by local GDP deflator 05  "
    label variable ms_wage_defGDP_05 "middle-skill wages  - . deflated by local GDP deflator 05  "

    label variable va_emp_defGDP_05 "labor productivity (VA/H_EMP) -  deflated by local GDP deflator 05 "



    *2.a.iii. Convert to USD using 2005 fixed exchange rates.
    * Generate the 2005 exchange rate for each country. 
    gen value_2005=exchangerate if year==2005 & code=="TOT"
    sort country year
    by country: egen mgo=total(value_2005)
    replace value_2005=mgo
    drop mgo

    * Divide for each of the deflated variables (pi or GDP deflator) by the exchange rate in 2005.
    foreach var in ls_wage lms_wage ms_wage hs_wage av_wage va_emp GDP_nom_own cap_GDP_nom_own { 

        gen `var'_pi_USD_05=`var'_pi_05/value_2005


        * And then we have to fix the conversion for the countries that joined the Euro later 
        * than 2005.  
        replace `var'_pi_USD_05=`var'_pi_USD_05*239.64 if country=="svn" 
        replace `var'_pi_USD_05=`var'_pi_USD_05*30.1260 if country=="svk" 
        replace `var'_pi_USD_05=`var'_pi_USD_05*0.429300 if country=="mlt" 
        replace `var'_pi_USD_05=`var'_pi_USD_05*0.585274 if country=="cyp" 
        replace `var'_pi_USD_05=`var'_pi_USD_05*15.6466 if country=="est" 


    }

    *Ltu is a special case because GDP data is always in euro and wage data always
    *in local currency. Thus, only have to multiply by last exchange rate of Euro /
    * local currecy for the GDP data. 
    replace GDP_nom_own_pi_USD_05=GDP_nom_own_pi_USD_05 *3.4528 if country=="ltu"
    replace cap_GDP_nom_own_pi_USD_05=cap_GDP_nom_own_pi_USD_05 *3.4528 if country=="ltu"

    foreach var in ls_wage lms_wage ms_wage hs_wage av_wage va_emp { 
        gen `var'_defGDP_USD_05=`var'_defGDP_05/value_2005

        * And then we have to fix the conversion for the countries that joined the Euro later 
        * than 2005. 
        replace `var'_defGDP_USD_05=`var'_defGDP_USD_05*239.64 if country=="svn" 
        replace `var'_defGDP_USD_05=`var'_defGDP_USD_05*30.1260 if country=="svk" 
        replace `var'_defGDP_USD_05=`var'_defGDP_USD_05*0.429300 if country=="mlt" 
        replace `var'_defGDP_USD_05=`var'_defGDP_USD_05*0.585274 if country=="cyp" 
        replace `var'_defGDP_USD_05=`var'_defGDP_USD_05*15.6466 if country=="est" 

    }


    * Label the variables. 
    label variable ls_wage_pi_USD_05 "low-skill wages  -  deflated by local pi and then converted using US 2005 exchange rate"
    label variable hs_wage_pi_USD_05 "high-skill wages  -  deflated by local pi and then converted using US 2005 exchange rate"
    label variable av_wage_pi_USD_05 "average wages  -  deflated by local pi and then converted using US 2005 exchange rate"
    label variable lms_wage_pi_USD_05 "low-skill/middle-skill wages  - deflated by local pi and then converted using US 2005 exchange rate"
    label variable ms_wage_pi_USD_05 "middle-skill wages  - deflated by local pi and then converted using US 2005 exchange rate"
    label variable va_emp_pi_USD_05 "labor productivity (VA/H_EMP)  -  deflated by local pi and then converted using US 2005 exchange rate"
    label variable GDP_nom_own_pi_USD_05 "Nominal GDP   -  deflated by local pi and then converted using US 2005 exchange rate"
    label variable cap_GDP_nom_own_pi_USD_05 "Nominal GDP per capita  -  deflated by local pi and then converted using US 2005 exchange rate"

    label variable ls_wage_defGDP_USD_05 "low-skill wages  -  deflated by local GDP deflator and then converted using US 2005 exchange rate"
    label variable hs_wage_defGDP_USD_05 "high-skill wages  -  deflated by local GDP deflator and then converted using US 2005 exchange rate"
    label variable av_wage_defGDP_USD_05 "average wages  -  deflated by local GDP deflator and then converted using US 2005 exchange rate"
    label variable lms_wage_defGDP_USD_05 "low-skill/middle-skill wages  - deflated by local GDP deflator and then converted using US 2005 exchange rate"
    label variable ms_wage_defGDP_USD_05 "middle-skill wages  - deflated by local GDP deflator and then converted using US 2005 exchange rate"
    label variable va_emp_defGDP_USD_05 "labor productivity (VA/H_EMP) -  deflated by local GDP deflator and then converted using US 2005 exchange rate"


    *Only keep the deflated and converted variables. 
    drop ls_wage_pi_05  hs_wage_pi_05  av_wage_pi_05 lms_wage_pi_05 ms_wage_pi_05 va_emp_pi_05 GDP_nom_own_pi_05 cap_GDP_nom_own_pi_05
    drop ls_wage_defGDP_05  hs_wage_defGDP_05  av_wage_defGDP_05 lms_wage_defGDP_05 ms_wage_defGDP_05 va_emp_defGDP_05


    ************************************************************************
    **2.b. Variables with 1995 fixed exchange rates (PPI & GDP deflator)****
    ************************************************************************
    *Deflated by PPI and local GDP deflator. 

    *First generate a renormalize VA_P with value 100 in 1995. 
    sort country code
    by country code: egen mgo=total(VA_P) if year==1995
    by country code: egen mgo2=total(mgo)
    gen VA_P_95=VA_P/mgo2*100
    drop mgo*

    *2.b.i. Defalte by PPI 
    foreach var in ls_wage lms_wage ms_wage hs_wage av_wage va_emp GDP_nom_own cap_GDP_nom_own code_VA { 
        gen `var'_pi_95=`var'/VA_P_95*100
    }

    label variable code_VA_pi_95 "valued added in manufacturing - deflated by pi.95"
    label variable ls_wage_pi_95 "low-skill wages  -  deflated by pi.95 "
    label variable hs_wage_pi_95 "high-skill wages  -  deflated by pi.95 "
    label variable av_wage_pi_95 "average wages  -  deflated by pi.95 "
    label variable lms_wage_pi_95 "low-skill/middle-skill wages  - . deflated by pi.95 "
    label variable ms_wage_pi_95 "middle-skill wages  - . deflated by pi.95 "
    label variable va_emp_pi_95 "labor productivity (VA/H_EMP)  -  deflated by pi.95 "
    label variable GDP_nom_own_pi_95 "Nominal GDP local currency  -  deflated by pi.95 "
    label variable cap_GDP_nom_own_pi_95 "Nominal GDP per capita local currency  -  deflated by pi.95 "

    *2.b.ii. Deflate by local GDP deflator. 
    foreach var in ls_wage lms_wage ms_wage hs_wage av_wage va_emp code_VA {
        gen `var'_defGDP_95=`var'/def_GDP_own_95*100
    }

    label variable code_VA_defGDP_95 "value added in manuf  -  deflated by local GDP deflator 95  "
    label variable ls_wage_defGDP_95 "low-skill wages  -  deflated by local GDP deflator 95  "
    label variable hs_wage_defGDP_95 "high-skill wages  -  deflated by local GDP deflator 95 "
    label variable av_wage_defGDP_95 "average wages  -  deflated by local GDP deflator 95 "
    label variable lms_wage_defGDP_95 "low-skill/middle-skill wages  - . deflated by local GDP deflator 05 "
    label variable ms_wage_defGDP_95 "middle-skill wages  - . deflated by local GDP deflator 95 "
    label variable va_emp_defGDP_95 "labor productivity (VA/H_EMP) -  deflated by local GDP deflator 95 "


    *2.b.iii. Convert to USD using 1995 fixed exchange rates.
    * Generate the 1995 exchange rate for each country. 
    gen value_1995=exchangerate if year==1995 & code=="TOT"
    sort country year
    by country: egen mgo=total(value_1995)
    replace value_1995=mgo
    drop mgo

    * save deflators for later
    preserve
        keep year country code value_1995 VA_P_95
        duplicates drop
        keep if code == "TOT" | code == "D"
        sort year country code
        save ${mow_data_proc}/exchange_rate_ppi.dta, replace
    restore

    * Divide for each of the deflated variables (pi or GDP deflator) by the exchange rate in 1995.
    foreach var in ls_wage lms_wage ms_wage hs_wage av_wage va_emp GDP_nom_own cap_GDP_nom_own code_VA { 

        gen `var'_pi_USD_95=`var'_pi_95/value_1995

        * And then we have to fix the conversion for the countries that joined the Euro later 
        * than 1995 (which is all EU countries). 
        replace `var'_pi_USD_95=`var'_pi_USD_95*239.64 if country=="svn" 
        replace `var'_pi_USD_95=`var'_pi_USD_95*30.1260 if country=="svk" 
        replace `var'_pi_USD_95=`var'_pi_USD_95*0.429300 if country=="mlt" 
        replace `var'_pi_USD_95=`var'_pi_USD_95*0.585274 if country=="cyp" 
        replace `var'_pi_USD_95=`var'_pi_USD_95*15.6466 if country=="est" 

        replace `var'_pi_USD_95=`var'_pi_USD_95*166.386 if country=="esp" 
        replace `var'_pi_USD_95=`var'_pi_USD_95*13.7603 if country=="aut" 
        replace `var'_pi_USD_95=`var'_pi_USD_95*40.3399 if country=="bel" 
        replace `var'_pi_USD_95=`var'_pi_USD_95*5.94573 if country=="fin" 
        replace `var'_pi_USD_95=`var'_pi_USD_95*6.55957 if country=="fra" 
        replace `var'_pi_USD_95=`var'_pi_USD_95*1.95583 if country=="deu" 
        replace `var'_pi_USD_95=`var'_pi_USD_95*340.750 if country=="grc" 
        replace `var'_pi_USD_95=`var'_pi_USD_95*0.787564 if country=="irl" 
        replace `var'_pi_USD_95=`var'_pi_USD_95*1936.27 if country=="ita" 
        replace `var'_pi_USD_95=`var'_pi_USD_95*2.20371 if country=="nld" 
        replace `var'_pi_USD_95=`var'_pi_USD_95*200.482 if country=="prt" 
        replace `var'_pi_USD_95=`var'_pi_USD_95*40.3399 if country=="lux"


    }

    *lithuania is a special case because GDP data is always in euro and wage data always
    *in local currency. Thus, only have to multiply by last exchange rate of Euro /
    * local currecy for the GDP data. 
    replace GDP_nom_own_pi_USD_95=GDP_nom_own_pi_USD_95 *3.4528 if country=="ltu"
    replace cap_GDP_nom_own_pi_USD_95=cap_GDP_nom_own_pi_USD_95 *3.4528 if country=="ltu"


    foreach var in ls_wage lms_wage ms_wage hs_wage av_wage va_emp code_VA { 

        gen `var'_defGDP_USD_95=`var'_defGDP_95/value_1995

        * And then we have to fix the conversion for the countries that joined the Euro later 
        * than 1995 (which is all EU countries). 
        replace `var'_defGDP_USD_95=`var'_defGDP_USD_95*239.64 if country=="svn" 

        replace `var'_defGDP_USD_95=`var'_defGDP_USD_95*30.1260 if country=="svk"
        replace `var'_defGDP_USD_95=`var'_defGDP_USD_95*0.429300 if country=="mlt" 
        replace `var'_defGDP_USD_95=`var'_defGDP_USD_95*0.585274 if country=="cyp" 
        replace `var'_defGDP_USD_95=`var'_defGDP_USD_95*15.6466 if country=="est" 

        replace `var'_defGDP_USD_95=`var'_defGDP_USD_95*166.386 if country=="esp" 
        replace `var'_defGDP_USD_95=`var'_defGDP_USD_95*13.7603 if country=="aut" 
        replace `var'_defGDP_USD_95=`var'_defGDP_USD_95*40.3399 if country=="bel" 
        replace `var'_defGDP_USD_95=`var'_defGDP_USD_95*5.94573 if country=="fin" 
        replace `var'_defGDP_USD_95=`var'_defGDP_USD_95*6.55957 if country=="fra" 
        replace `var'_defGDP_USD_95=`var'_defGDP_USD_95*1.95583 if country=="deu" 
        replace `var'_defGDP_USD_95=`var'_defGDP_USD_95*340.750 if country=="grc" 
        replace `var'_defGDP_USD_95=`var'_defGDP_USD_95*0.787564 if country=="irl" 
        replace `var'_defGDP_USD_95=`var'_defGDP_USD_95*1936.27 if country=="ita" 
        replace `var'_defGDP_USD_95=`var'_defGDP_USD_95*2.20371 if country=="nld" 
        replace `var'_defGDP_USD_95=`var'_defGDP_USD_95*200.482 if country=="prt" 
        replace `var'_defGDP_USD_95=`var'_defGDP_USD_95*40.3399 if country=="lux"

    }



    * Label the variables. 
    label variable code_VA_pi_USD_95 "value added in manuf -  deflated by local pi and then converted using US 1995 exchange rate"
    label variable ls_wage_pi_USD_95 "low-skill wages  -  deflated by local pi and then converted using US 1995 exchange rate"
    label variable hs_wage_pi_USD_95 "high-skill wages  -  deflated by local pi and then converted using US 1995 exchange rate"
    label variable av_wage_pi_USD_95 "average wages  -  deflated by local pi and then converted using US 1995 exchange rate"
    label variable lms_wage_pi_USD_95 "low-skill/middle-skill wages  - deflated by local pi and then converted using US 1995 exchange rate"
    label variable ms_wage_pi_USD_95 "middle-skill wages  - deflated by local pi and then converted using US 1995 exchange rate"
    label variable va_emp_pi_USD_95 "labor productivity (VA/H_EMP)  -  deflated by local pi and then converted using US 1995 exchange rate"
    label variable GDP_nom_own_pi_USD_95 "Nominal GDP -  deflated by local pi and then converted using US 1995 exchange rate"
    label variable cap_GDP_nom_own_pi_USD_95 "Nominal GDP per capita  -  deflated by local pi and then converted using US 1995 exchange rate"

    label variable code_VA_defGDP_USD_95 "value added in manuf  -  deflated by local GDP deflator and then converted using US 1995 exchange rate"
    label variable ls_wage_defGDP_USD_95 "low-skill wages  -  deflated by local GDP deflator and then converted using US 1995 exchange rate"
    label variable hs_wage_defGDP_USD_95 "high-skill wages  -  deflated by local GDP deflator and then converted using US 1995 exchange rate"
    label variable av_wage_defGDP_USD_95 "average wages  -  deflated by local GDP deflator and then converted using US 1995 exchange rate"
    label variable lms_wage_defGDP_USD_95 "low-skill/middle-skill wages  - deflated by local GDP deflator and then converted using US 1995 exchange rate"
    label variable ms_wage_defGDP_USD_95 "middle-skill wages  - deflated by local GDP deflator and then converted using US 1995 exchange rate"
    label variable va_emp_defGDP_USD_95 "labor productivity (VA/H_EMP) -  deflated by local GDP deflator and then converted using US 1995 exchange rate"



    *Only keep the deflated and converted variables. 
    drop ls_wage_pi_95  hs_wage_pi_95  av_wage_pi_95 lms_wage_pi_95 ms_wage_pi_95 va_emp_pi_95 GDP_nom_own_pi_95 cap_GDP_nom_own_pi_95 code_VA_pi_95
    drop ls_wage_defGDP_95  hs_wage_defGDP_95  av_wage_defGDP_95 lms_wage_defGDP_95 ms_wage_defGDP_95 va_emp_defGDP_95 code_VA_defGDP_95




    ************************************************************************
    **2.c. Variables deflated by PPI total US & every year exchange rate ***
    ************************************************************************

    *2.c.i. Exchange rate Conversion
    *Divide by exchange rate for each year. 

    foreach var in ls_wage lms_wage ms_wage hs_wage av_wage va_emp  GDP_nom_own cap_GDP_nom_own {

        gen `var'_USD=`var'/exchangerate

        * For Eurozone countries we have to convert to original currency before year of admission to EURO. 

        replace `var'_USD=`var'_USD*166.386 if country=="esp" & year<1999
        replace `var'_USD=`var'_USD*13.7603 if country=="aut" & year<1999
        replace `var'_USD=`var'_USD*40.3399 if country=="bel" & year<1999
        replace `var'_USD=`var'_USD*5.94573 if country=="fin" & year<1999
        replace `var'_USD=`var'_USD*6.55957 if country=="fra" & year<1999
        replace `var'_USD=`var'_USD*1.95583 if country=="deu" & year<1999
        replace `var'_USD=`var'_USD*340.750 if country=="grc" & year<2001
        replace `var'_USD=`var'_USD*0.787564 if country=="irl" & year<1999
        replace `var'_USD=`var'_USD*1936.27 if country=="ita" & year<1999
        replace `var'_USD=`var'_USD*2.20371 if country=="nld" & year<1999
        replace `var'_USD=`var'_USD*200.482 if country=="prt" & year<1999
        replace `var'_USD=`var'_USD*40.3399 if country=="lux" & year<1999


        replace `var'_USD=`var'_USD*239.64 if country=="svn" & year<2007



        replace `var'_USD=`var'_USD*30.1260 if country=="svk" & year<2009


        replace `var'_USD=`var'_USD*0.429300 if country=="mlt" & year<2008
        replace `var'_USD=`var'_USD*0.585274 if country=="cyp" & year<2008
        replace `var'_USD=`var'_USD*15.6466 if country=="est" & year<2011

    }

    *Ltu is a special case because GDP data is always in euro and wage data always
    *in local currency. Thus, only have to multiply by last exchange rate of Euro /
    *local currecy for the GDP data. 
    replace GDP_nom_own_USD=GDP_nom_own_USD *3.4528 if country=="ltu" & year<2015
    replace cap_GDP_nom_own_USD=cap_GDP_nom_own_USD *3.4528 if country=="ltu" & year<2015


    *Lable the variables. 
    label variable ls_wage_USD "low-skill wages - . yearly US dollar conversion. "
    label variable hs_wage_USD "high-skill wages - . yearly US dollar conversion. "
    label variable lms_wage_USD "low-skill/middle-skill wages - . yearly US dollar conversion. "
    label variable ms_wage_USD "middle-skill wages - . yearly US dollar conversion. "
    label variable av_wage_USD "average wages - . yearly US dollar conversion. "
    label variable va_emp_USD "labor productivity (VA/H_EMP) - . yearly US dollar conversion. "
    label variable GDP_nom_own_USD "Nominal GDP  - . yearly US dollar conversion. "
    label variable cap_GDP_nom_own_USD "Nominal GDP per capita  - . yearly US dollar conversion. "


    *2.c.ii Generate the US pi deflator. (VA_P_USA) (The VA_P with value 100 in 2005 is used) 
    sort year code country 
    by year code: egen mgo2=total(VA_P) if country=="usa"
    by year code: egen VA_P_USA=total(mgo2)
    drop mgo2

    label variable VA_P_USA "US pi deflator"


    *Divide yearly US dollar variables trough US pi deflator. 
    foreach var in ls_wage lms_wage ms_wage hs_wage av_wage va_emp GDP_nom_own cap_GDP_nom_own {
        gen `var'_USD_pi=`var'_USD/VA_P_USA*100
    }



    *Label the variables. 
    label variable ls_wage_USD_pi "low-skill wages - . yearly US dollar conversion deflated by US pi "
    label variable hs_wage_USD_pi "high-skill wages - . yearly US dollar conversion deflated by US pi. "
    label variable lms_wage_USD_pi "low-skill/middle-skill wages - . yearly US dollar conversion deflated by US pi "
    label variable ms_wage_USD_pi "middle-skill wages - . yearly US dollar conversion deflated by US pi "
    label variable av_wage_USD_pi "average wages - . yearly US dollar conversion deflated by US pi "
    label variable va_emp_USD_pi "labor productivity (VA/H_EMP) - . yearly US dollar conversion deflated by US pi "
    label variable GDP_nom_own_USD_pi "nominal GDP - . yearly US dollar conversion deflated by US pi "
    label variable cap_GDP_nom_own_USD_pi "nominal GDP per capita - . yearly US dollar conversion deflated by US pi "


    *2.c.iii Generate the US GDP deflator. (def_GDP_USA) (The def_GDP_own_05 with value 100 in 2005 is used) 
    sort year code country 
    by year code: egen mgo2=total(def_GDP_own_05) if country=="usa"
    by year code: egen defGDP_USA=total(mgo2)
    drop mgo2

    label variable defGDP_USA "US GDP deflator"

    *Divide yearly US dollar variables trough US GDP deflator. 
    foreach var in GDP_nom_own cap_GDP_nom_own  {
        gen `var'_USD_defGDP=`var'_USD/defGDP_USA*100
    }


    *Label the variables.

    label variable GDP_nom_own_USD_defGDP "Nominal GDP  - . yearly US dollar conversion deflated by US GDP deflator "
    label variable cap_GDP_nom_own_USD_defGDP "Nominal GDP per capita  - . yearly US dollar conversion deflated by US GDP deflator "

    *Only keep the deflated and converted variables. 
    drop ls_wage_USD  hs_wage_USD  av_wage_USD lms_wage_USD ms_wage_USD va_emp_USD GDP_nom_own_USD cap_GDP_nom_own_USD



    ***************************************
    **2.d. Real GDP (total & capita) converted into USD (1995 and 2005) ***/
    **************************************/
    *Generate real GDP (total and per capita) in local currency converted in USD (1995 and 2005)

    *2.d.i. 2005
    foreach var in GDP_real_own_05 cap_GDP_real_own_05 {

        gen `var'_USD_05 = `var' / value_2005


        * And then we have to fix the conversion for the countries that joined the Euro later 
        * than 2005.  
        replace `var'_USD_05=`var'_USD_05*239.64 if country=="svn" 
        replace `var'_USD_05=`var'_USD_05*30.1260 if country=="svk" 
        replace `var'_USD_05=`var'_USD_05*0.429300 if country=="mlt" 
        replace `var'_USD_05=`var'_USD_05*0.585274 if country=="cyp" 
        replace `var'_USD_05=`var'_USD_05*15.6466 if country=="est"
        replace `var'_USD_05=`var'_USD_05*3.4528 if country=="ltu"  
    }


    *2.d.ii. 1995
    foreach var in GDP_real_own_95 cap_GDP_real_own_95 {

        gen `var'_USD_95 = `var' / value_1995


        * And then we have to fix the conversion for the countries that joined the Euro later 
        * than 1995.  
        replace `var'_USD_95=`var'_USD_95*239.64 if country=="svn" 
        replace `var'_USD_95=`var'_USD_95*30.1260 if country=="svk" 
        replace `var'_USD_95=`var'_USD_95*0.429300 if country=="mlt" 
        replace `var'_USD_95=`var'_USD_95*0.585274 if country=="cyp" 
        replace `var'_USD_95=`var'_USD_95*15.6466 if country=="est" 
        replace `var'_USD_95=`var'_USD_95*3.4528 if country=="ltu" 

        replace `var'_USD_95=`var'_USD_95*166.386 if country=="esp" 
        replace `var'_USD_95=`var'_USD_95*13.7603 if country=="aut" 
        replace `var'_USD_95=`var'_USD_95*40.3399 if country=="bel" 
        replace `var'_USD_95=`var'_USD_95*5.94573 if country=="fin" 
        replace `var'_USD_95=`var'_USD_95*6.55957 if country=="fra" 
        replace `var'_USD_95=`var'_USD_95*1.95583 if country=="deu" 
        replace `var'_USD_95=`var'_USD_95*340.750 if country=="grc" 
        replace `var'_USD_95=`var'_USD_95*0.787564 if country=="irl" 
        replace `var'_USD_95=`var'_USD_95*1936.27 if country=="ita" 
        replace `var'_USD_95=`var'_USD_95*2.20371 if country=="nld" 
        replace `var'_USD_95=`var'_USD_95*200.482 if country=="prt" 
        replace `var'_USD_95=`var'_USD_95*40.3399 if country=="lux"
    }



    *Label the variables 
    label variable GDP_real_own_05_USD_05 "real GDP (constant 2005 prices) -  converted using US 2005 exchange rate"
    label variable GDP_real_own_95_USD_95 "real GDP (constant 1995 prices) -  converted using US 1995 exchange rate"
    label variable cap_GDP_real_own_05_USD_05 "real GDP per capita (constant 2005 prices) -  converted using US 2005 exchange rate"
    label variable cap_GDP_real_own_95_USD_95 "real GDP per capita  (constant 1995 prices) -  converted using US 1995 exchange rate"



    ***************************************
    **2.e. Robustness variables  **********
    ***************************************


    *For robustness check generate value of total low skill wages for all years
    *using 1995 USD conversion rate (and no deflation). 

    * Divide the low skill wage by the exchange rate in 1995.

    gen ls_wage_USD_95=ls_wage/value_1995


    * And then we have to fix the conversion for the countries that joined the Euro later 
    * than 1995 (which is all EU countries). 
    replace ls_wage_USD_95=ls_wage_USD_95*239.64 if country=="svn" 
    replace ls_wage_USD_95=ls_wage_USD_95*30.1260 if country=="svk" 
    replace ls_wage_USD_95=ls_wage_USD_95*0.429300 if country=="mlt" 
    replace ls_wage_USD_95=ls_wage_USD_95*0.585274 if country=="cyp" 
    replace ls_wage_USD_95=ls_wage_USD_95*15.6466 if country=="est" 

    replace ls_wage_USD_95=ls_wage_USD_95*166.386 if country=="esp" 
    replace ls_wage_USD_95=ls_wage_USD_95*13.7603 if country=="aut" 
    replace ls_wage_USD_95=ls_wage_USD_95*40.3399 if country=="bel" 
    replace ls_wage_USD_95=ls_wage_USD_95*5.94573 if country=="fin" 
    replace ls_wage_USD_95=ls_wage_USD_95*6.55957 if country=="fra" 
    replace ls_wage_USD_95=ls_wage_USD_95*1.95583 if country=="deu" 
    replace ls_wage_USD_95=ls_wage_USD_95*340.750 if country=="grc" 
    replace ls_wage_USD_95=ls_wage_USD_95*0.787564 if country=="irl" 
    replace ls_wage_USD_95=ls_wage_USD_95*1936.27 if country=="ita" 
    replace ls_wage_USD_95=ls_wage_USD_95*2.20371 if country=="nld" 
    replace ls_wage_USD_95=ls_wage_USD_95*200.482 if country=="prt" 
    replace ls_wage_USD_95=ls_wage_USD_95*40.3399 if country=="lux"

    label variable ls_wage_USD_95 "low-skill wages - converted using 1995 USD exchange rate"





    *Also generate the total low skill wages times the hours worked for the year 1995. 
    *I checked if we need to look at the WIOD and the EUklems data for this. But we do not.
    *WIOD and EUklems do have this data and they do differ in 1995. But when we want to 
    *follow the same procedure as we did for the wages, va_emp etc. we used in 1995 the
    *value of WIOD and adapted the values for previous years accoringly such that the variable
    *path is smooth. Thus, since we here only need the 1995 value, we do not have to 
    *look at WIOD and EUklems. We also only need it for D. This is created in the load_data_WIOD.do
    *file (only for D). We created there the variable H_LS_TOT_WIOD = H_LS * H_EMP
    *which is exactelly what we need. Thus use this. 
    *The only country which is missing is Switzerland. So we add it by hand. 

    *Total hours times low skill wages 
    gen tot_pay_ls = H_LS_TOT_WIOD * ls_wage *1000000 if year==1995 & code=="D"

    *Add Switzerland by hand. (Use the data from "employmentswitzerland" and "UNI_ZH_version_vs_MGO" in the 
    *"swiss data" folder. Use employment in 1995 in manufacturing (average over the 4 quarters) times the share
    *of population that is low skill workers in manufacturing (average from 1996 and 1994)
    *times the monthly salary times 12. 
    replace tot_pay_ls = 8144423434 if year==1995 & code=="D" & country=="che"


    * Divide it by the exchange rate in 1995.

    gen tot_pay_ls_USD_95 =tot_pay_ls/value_1995 



    * And then we have to fix the conversion for the countries that joined the Euro later 
    * than 1995 (which is all EU countries). (totlsw in import_indepvars.do)
    replace tot_pay_ls_USD_95=tot_pay_ls_USD_95*239.64 if country=="svn" 
    replace tot_pay_ls_USD_95=tot_pay_ls_USD_95*30.1260 if country=="svk" 
    replace tot_pay_ls_USD_95=tot_pay_ls_USD_95*0.429300 if country=="mlt" 
    replace tot_pay_ls_USD_95=tot_pay_ls_USD_95*0.585274 if country=="cyp" 
    replace tot_pay_ls_USD_95=tot_pay_ls_USD_95*15.6466 if country=="est" 

    replace tot_pay_ls_USD_95=tot_pay_ls_USD_95*166.386 if country=="esp" 
    replace tot_pay_ls_USD_95=tot_pay_ls_USD_95*13.7603 if country=="aut" 
    replace tot_pay_ls_USD_95=tot_pay_ls_USD_95*40.3399 if country=="bel" 
    replace tot_pay_ls_USD_95=tot_pay_ls_USD_95*5.94573 if country=="fin" 
    replace tot_pay_ls_USD_95=tot_pay_ls_USD_95*6.55957 if country=="fra" 
    replace tot_pay_ls_USD_95=tot_pay_ls_USD_95*1.95583 if country=="deu" 
    replace tot_pay_ls_USD_95=tot_pay_ls_USD_95*340.750 if country=="grc" 
    replace tot_pay_ls_USD_95=tot_pay_ls_USD_95*0.787564 if country=="irl" 
    replace tot_pay_ls_USD_95=tot_pay_ls_USD_95*1936.27 if country=="ita" 
    replace tot_pay_ls_USD_95=tot_pay_ls_USD_95*2.20371 if country=="nld" 
    replace tot_pay_ls_USD_95=tot_pay_ls_USD_95*200.482 if country=="prt" 
    replace tot_pay_ls_USD_95=tot_pay_ls_USD_95*40.3399 if country=="lux"

    drop tot_pay_ls
    label variable tot_pay_ls_USD_95 "total payments to low-skill workers converted using 1995 USD exchange rate"

    save ${final_dir}/wage_combined_final_allcodes.dta, replace


}
if _rc == 0 {
    display "Execution finished successfully."
}
else {
    display "Execution finished with errors."
}

cap log close dat